﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Framework.Util
{
    public sealed class Reflection
    {
        /// <summary>
        /// 获取单个类特性，如有多个，将返回第一个
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <returns></returns>
        public static T GetClassAttribute<T>(Type type) where T : Attribute
        {
            T[] attribute = GetClassAttributes<T>(type);
            if (attribute == null || attribute.Count() <= 0)
                return null;
            return attribute[0];
        }

        /// <summary>
        /// 获取多个类特性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <returns></returns>
        public static T[] GetClassAttributes<T>(Type type) where T : Attribute
        {
            return type.GetCustomAttributes(typeof(T), false) as T[];
        }
    }
}
